package com.example.kotlinstudy.类与对象

class Address {
    //var声明为可变；val声明为只读
    var name: String = "name";
    var street: String? = null;
}

fun copyAddress(address: Address): Address {
    val result = Address()
    result.name = address.name;
    return result
}

///getter
class RectangleObject(val width: Int, val height: Int) {

    //    val area: Int
//        get() = this.width * this.height
    val area
        get() = this.width * this.height
    var areaText: String = ""
    var stringRepresentation: String
        get() = this.toString();
        set(value) {
            this.areaText = value
        }
    var setterVisibility: String = "abc"
        private set

//    var setterWithAnnotation: Any? = null
//        @Inject set // 用 Inject 注解此 setter

    //幕后字段
    var counter = 0
        set(value) {
            if (value >= 0)
                field = value
        }
    val isEmpty: Boolean
        get() = this.counter == 0
}


class MyTest {
    //延迟初始化属性与变量
    lateinit var subject: TestSubject

    @SetUp
    fun setup() {
        subject = TestSubject()
    }

    @Test
    fun test() {
        subject.method()  // 直接解引用
    }
}

//编译期常量
//const val SUBSYSTEM_DEPRECATED: String = "This subsystem is deprecated"
//@Deprecated(SUBSYSTEM_DEPRECATED)
//fun foo() {}

fun main() {
    val rectangle = RectangleObject(3, 4);
    println("Width=${rectangle.width}, height=${rectangle.height}, area=${rectangle.area}")

    //检测一个 lateinit var 是否已初始化
//    if (foo::bar.isInitialized) {
//        println(foo.bar)
//    }
}